package com.wiz.weixin.controllor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wiz.weixin.common.ResponseCode;
import com.wiz.weixin.manager.TencentManager;
import com.wiz.weixin.service.StationService;

/**
 * 油站相关信息
 */
@Controller
@RequestMapping("/station")
public class StationControllor implements ResponseCode{
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private TencentManager tencentManager;
	@Resource
	private StationService stationService;
	
	@RequestMapping(value="/fp/price")
	public String list(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws Exception {
		String openId = (String) session.getAttribute("openid");
//		String openId = "oMrj_w4eX8IODYrHdAp79W0rl_88";
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
		}
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "不能得到你的微信编码！");
			return "/error";
		}
		session.setAttribute("openid", openId);
		String s = stationService.getFpPriceListByStation(openId,model);
		if(StringUtils.isNotBlank(s)){
			return s;
		}
		return "/station/price";
	}
	
	@RequestMapping(value="/charge")
	public String chargeList(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws Exception {
		String openId = (String) session.getAttribute("openid");
//		String openId = "oMrj_w4eX8IODYrHdAp79W0rl_88";
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
		}
		log.info("-----------------------------"+openId);
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "不能得到你的微信编码！");
			return "/error";
		}
		session.setAttribute("openid", openId);
		String s = stationService.getChargeTotalByStation(openId,model);
		if(StringUtils.isNotBlank(s)){
			return s;
		}
		return "/station/charge";
	}
	
	@RequestMapping(value="/today/fuelling")
	public String fuellingTodayList(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws Exception {
		String openId = (String) session.getAttribute("openid");
//		String openId = "oMrj_w4eX8IODYrHdAp79W0rl_88";
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
		}
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "不能得到你的微信编码！");
			return "/error";
		}
		session.setAttribute("openid", openId);
		String s = stationService.getFuellingTotalByStationInToday(openId,model);
		if(StringUtils.isNotBlank(s)){
			return s;
		}
		return "/station/fuelling";
	}
	
	@RequestMapping(value="/yestoday/fuelling")
	public String fuellingYestodayList(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws Exception {
		String openId = (String) session.getAttribute("openid");
//		String openId = "oMrj_w4eX8IODYrHdAp79W0rl_88";
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
		}
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "不能得到你的微信编码！");
			return "/error";
		}
		session.setAttribute("openid", openId);
		String s = stationService.getFuellingTotalByStationInYestoday(openId, model);
		if(StringUtils.isNotBlank(s)){
			return s;
		}
		return "/station/fuellingy";
	}
	
	@RequestMapping(value="/oiltank")
	public String oilTankList(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws Exception {
		String openId = (String) session.getAttribute("openid");
//		String openId = "oMrj_w4eX8IODYrHdAp79W0rl_88";
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
		}
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "不能得到你的微信编码！");
			return "/error";
		}
		session.setAttribute("openid", openId);
		String s = stationService.getOilTankListByStation(openId,model);
		if(StringUtils.isNotBlank(s)){
			return s;
		}
		return "/station/oiltank";
	}
	
	@RequestMapping(value="/group/now")
	public String fuellingNowGroupList(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws Exception {
		String openId = (String) session.getAttribute("openid");
//		String openId = "oMrj_w4eX8IODYrHdAp79W0rl_88";
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
		}
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "不能得到你的微信编码！");
			return "/error";
		}
		session.setAttribute("openid", openId);
		String s = stationService.getFuellingByThisGroup(openId, model);
		if(StringUtils.isNotBlank(s)){
			return s;
		}
		return "/station/fuellingtg";
	}
	
	@RequestMapping(value="/group/last")
	public String fuellingLastGroupList(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws Exception {
		String openId = (String) session.getAttribute("openid");
//		String openId = "oMrj_w4eX8IODYrHdAp79W0rl_88";
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
		}
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "不能得到你的微信编码！");
			return "/error";
		}
		session.setAttribute("openid", openId);
		String s = stationService.getFuellingByLastGroup(openId, model);
		if(StringUtils.isNotBlank(s)){
			return s;
		}
		return "/station/fuellinglg";
	}
}
